/**
 * @class   wosh::devices::VGettyAMBundle
 * @brief   \ref wosh::interfaces::services::CommunicatorService "Communicator Service"
 *          and wrapper for \c vgetty (POSIX only): an simple Answering Machine
 *          compatible with many 56k modems.
 *
 * @ingroup Devices
 ****************************************************************************
 * @version $Id: VGettyAMBundle.h 2288 2010-04-09 08:33:26Z alex $
 * @author  Alessandro Polo
 * @date    Released on 2009-06-04
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Bundles_Devices_VGettyAM_H__
 #define __WOSH_Bundles_Devices_VGettyAM_H__

 #include <core/BundleGeneric.h>
 #include <core/Thread.h>

 #include "IVGettyAMListener.h"
 
 #include <core/Communicator.h>
 #include <interfaces/Device.h>
 #include <interfaces/devices/Phone.h>
 #include <interfaces/devices/VGettyAM.h>
 #include <interfaces/services/CommunicatorService.h>

 #include <framework/communication/AudioMessage.h>
 #include <framework/communication/TextMessage.h>
 #include <framework/communication/VoiceCall.h>

class VGettyAMImpl;

namespace wosh {
 namespace devices {

 #define _VGettyAM_NAME							"VGettyAM"
 #define _VGettyAM_VERSION						0.62

 #define _VGettyAM_DEFAULT_ScriptDial			"/usr/sbin/vgetty-dial.sh"
 #define _VGettyAM_DEFAULT_ScriptControl		"/usr/sbin/vgetty-ctrl.sh"
 
 #define _VGettyAM_DEFAULT_FolderStored			"/var/spool/voice/messages"
 #define _VGettyAM_DEFAULT_FolderIncoming		"/var/spool/voice/incoming"

 #define _VGettyAM_DEFAULT_MessageGreeting		"greeting.rmd"

 #define _VGettyAM_DEFAULT_BindAddressSlave		"192.168.0.2:7902"
 #define _VGettyAM_DEFAULT_BindAddressDevice	"0.0.0.0:7901"


class VGettyAMBundle :	public wosh::BundleGeneric,
						public virtual wosh::interfaces::services::CommunicatorService,
						public virtual wosh::interfaces::devices::Phone,
						public virtual IVGettyAMListener
 {
	WOSH_BUNDLE(wosh::devices::VGettyAMBundle)
	WOSH_BUNDLE_INTERFACE("wosh::interfaces::devices::Phone")
	WOSH_BUNDLE_SINGLETON(0,0)


	public:
		/**
		 * @brief   Empty constructor, init bundle and vars, Properties and Methods.
		 */
		VGettyAMBundle();
		/**
		 * @brief   Empty destructor, may (synch) stop bundle and internal threads when still running
		 */
		virtual ~VGettyAMBundle();


	public:
		WRESULT bundleStart();
		WRESULT bundleStop();
		WRESULT bundleDiagnostic();

		bool updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source );

		void busMessage( const Message& message, const Bus* source );

//@}
/** @name Device Interface
 * @{
 ******************************************************************************/
	public:
		virtual wosh::interfaces::Device::DEVICE_STATE getDeviceState() const { return this->devState; }

//@}
/** @name Communicator Interface
 * @{
 ******************************************************************************/
	public:
		bool isUserReachable( const std::string& wosh_username );
		WRESULT sendMessageToUser( const std::string& wosh_username, const std::string& message );
		WRESULT sendMessageTo( const std::string& user_phonenumber, const std::string& message );
		WRESULT notify( const Notification* notification );

//@}
/** @name IVGettyAMListener Interface
 * @{
 ******************************************************************************/
	public:
		void incoming_DTMF( int line, const std::string& sequence, VGettyAMImpl* source );
		void incoming_Message( int line, const wosh::communication::AudioMessage* message, VGettyAMImpl* source );

		void phone_event( int line, int event, VGettyAMImpl* source );

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:
		std::string getPhoneNumberFromUsername( const std::string& wosh_username );
		std::string getUsernameFromPhoneNumber( const std::string& phone_number );

		void fixPhoneNumber( std::string& phone ) const;

		void raiseEvent( MessageContent* content, SecurityToken* security = NULL );

//@}
	private:
		Response* mmDoForceState( const Request* request );
		Response* mmDoRaiseEvent( const Request* request );

		Response* mmDoDialCall( const Message* requestMessage );
		Response* mmDoHangUpCall( const Message* requestMessage );
		Response* mmDoAnswerCall( const Message* requestMessage );

		Response* mmDoDialCallAndPlay( const Message* requestMessage );

		Response* mmDoSendMessageToUser( const Message* requestMessage );

		WRESULT declareCommunicator();

	protected:
		BusConnector BusDevices;	///< register myself & work as Device too
		VGettyAMImpl* devWorker;	///< real implementation

		wosh::interfaces::Device::DEVICE_STATE devState;		///< current state of device


}; // class def

 }; // namespace devices
}; // namespace wosh

#endif //__WOSH_Bundles_Devices_VGettyAM_H__
